package com.jnevision.attendancegroup.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnevision.attendancegroup.model.AttendanceGroup;
import com.jnevision.attendancegroup.model.AttendanceVo;
import com.jnevision.attendancegroup.service.AttendanceGroupServiceImpl;
import com.jnevision.user.model.User;
import com.jnevision.user.service.UserServiceImpl;
import com.jnevision.util.DateUtil;

/**
 * <p>
 * 考勤组 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-21
 */

@RestController
@RequestMapping("/attendancegroups")
public class AttendanceGroupController extends ApiController {

    @Autowired
    private AttendanceGroupServiceImpl attendanceGroupServiceImpl;
    @Autowired
    private UserServiceImpl userServiceImpl;

    @GetMapping
    public R<IPage<AttendanceGroup>> page(Page<AttendanceGroup> page) {
        return this.success(this.attendanceGroupServiceImpl.page(page));
    }

    /**
     * 添加考勤组
     * 
     * @param attendanceGroup
     * @return
     */
    @PostMapping
    public R<Integer> save(@RequestBody AttendanceGroup attendanceGroup) {
        if (null != attendanceGroup.getId()) {
            AttendanceGroup byId = this.attendanceGroupServiceImpl.getById(attendanceGroup.getId());
            if (!byId.getAttendanceCycle().equals(attendanceGroup.getAttendanceCycle())) {
                attendanceGroup.setCreateTime(DateUtil.getNormalTime());
                if (StringUtils.isEmpty(attendanceGroup.getReplaceCardNum())) {
                    attendanceGroup.setReplaceCardNum("0");
                }
                if (StringUtils.isEmpty(attendanceGroup.getLeaveDuration())) {
                    attendanceGroup.setLeaveDuration("0");
                }
                if (StringUtils.isEmpty(attendanceGroup.getAbsenteeism())) {
                    attendanceGroup.setAbsenteeism("0");
                }
                this.attendanceGroupServiceImpl.save(attendanceGroup);

            } else {
                attendanceGroup.setCreateTime(DateUtil.getNormalTime());
                attendanceGroup.setReplaceCardNum(byId.getReplaceCardNum());
                attendanceGroup.setLeaveDuration(byId.getLeaveDuration());
                attendanceGroup.setAbsenteeism(byId.getAbsenteeism());
                this.attendanceGroupServiceImpl.updateById(attendanceGroup);
            }
        } else {
            if (this.attendanceGroupServiceImpl
                .getOne(new QueryWrapper<AttendanceGroup>().eq("dept_id", attendanceGroup.getDeptId())
                    .eq("attendance_cycle", attendanceGroup.getAttendanceCycle())) != null) {
                return this.failed("已经存在该部门的排班，请去修改");
            }

            attendanceGroup.setCreateTime(DateUtil.getNormalTime());
            if (StringUtils.isEmpty(attendanceGroup.getReplaceCardNum())) {
                attendanceGroup.setReplaceCardNum("0");
            }
            if (StringUtils.isEmpty(attendanceGroup.getLeaveDuration())) {
                attendanceGroup.setLeaveDuration("0");
            }
            if (StringUtils.isEmpty(attendanceGroup.getAbsenteeism())) {
                attendanceGroup.setAbsenteeism("0");
            }
            this.attendanceGroupServiceImpl.save(attendanceGroup);

        }
        return R.ok(attendanceGroup.getId());

    }

    @PutMapping
    public R<Integer> update(@RequestBody AttendanceGroup attendanceGroup) {

        AttendanceGroup byId = this.attendanceGroupServiceImpl.getById(attendanceGroup.getId());

        attendanceGroup.setCreateTime(DateUtil.getNormalTime());
        attendanceGroup.setReplaceCardNum(byId.getReplaceCardNum());
        attendanceGroup.setLeaveDuration(byId.getLeaveDuration());
        attendanceGroup.setAbsenteeism(byId.getAbsenteeism());

        this.attendanceGroupServiceImpl.updateById(attendanceGroup);

        return R.ok(attendanceGroup.getId());
    }

    /**
     * 保存考勤核算规则设置
     * 
     * @param vo
     * @return
     */
    @PostMapping("/rule")
    public R<Boolean> saveRule(@RequestBody AttendanceVo vo) {

        return this.success(this.attendanceGroupServiceImpl.saveAttendance(vo));
    }

    @GetMapping("/{id}")
    public R<AttendanceGroup> getById(@PathVariable String id) {
        return this.success(this.attendanceGroupServiceImpl.getById(id));
    }

    @GetMapping("/dept")
    public R<List<AttendanceVo>> listByDeptId(Integer deptId, String attendanceCycle) {
        AttendanceGroup one = this.attendanceGroupServiceImpl
            .getOne(new QueryWrapper<AttendanceGroup>().eq("dept_id", deptId).eq("attendance_cycle", attendanceCycle));

        if (one == null) {
            List<User> userList = this.userServiceImpl.getDeptUser(deptId);
            List<AttendanceVo> attendList = new ArrayList<>();
            for (User user : userList) {
                AttendanceVo attendance = new AttendanceVo();
                attendance.setUserId(user.getId());
                attendance.setUserName(user.getName());
                attendance.setPhone(user.getPhone());
                attendance.setDeptId(deptId);
                attendance.setPhoneNum(user.getPhone());
                attendList.add(attendance);
            }

            return this.success(attendList);
        }

        return this.success(this.attendanceGroupServiceImpl.listByGroupId(one.getId()));
    }

    @DeleteMapping
    public R<Boolean> delete(Integer id) {

        return this.success(this.attendanceGroupServiceImpl.deleteById(id));
    }
}
